/*选项索引键值对*/
var optionsKey={
		"1":"A","2":"B","3":"C","4":"D","5":"E","6":"F","7":"G","8":"H","9":"I","10":"J","11":"K","12":"L","13":"M","14":"N","15":"O","16":"P","17":"Q","18":"R","19":"S","20":"T","21":"U","22":"V","23":"W","24":"X","25":"Y","26":"Z"
};
/*填空宽度控制*/
var textWidth = function(text){ 
    var sensor = $('<pre>'+ text +'</pre>').css({display: 'none'}); 
    $('body').append(sensor); 
    var width = sensor.width();
    sensor.remove();
    return width;
};
function changeText(obj){
    $(obj).width(textWidth($(obj).val())+30);
}

/* 创建编辑器  */
var E = window.wangEditor;
var editor = new E('#content-toolbar','#content');
editor.customConfig.uploadImgMaxSize = 6 * 1024 * 1024;
editor.customConfig.uploadImgMaxLength = 5;
editor.customConfig.uploadFileName = 'myFileName';
editor.customConfig.uploadImgHeaders = {
       'Accept' : 'multipart/form-data'
   };
editor.customConfig.menus = [//定义菜单
	'head',
	'image'
];
editor.customConfig.uploadImgServer = '/exam/files/upload';
editor.customConfig.showLinkImg = false;
editor.create();
/*创建reference编辑器*/
var ER = window.wangEditor;
var editorR = new ER('#reference-toolbar','#reference-content');
editorR.customConfig.uploadImgMaxSize = 6 * 1024 * 1024;
editorR.customConfig.uploadImgMaxLength = 5;
editorR.customConfig.uploadFileName = 'myFileName';
editorR.customConfig.uploadImgHeaders = {
       'Accept' : 'multipart/form-data'
   };
editorR.customConfig.menus = [//定义菜单
	'head',
	'image'
];
editorR.customConfig.uploadImgServer = '/exam/files/upload';
editorR.customConfig.showLinkImg = false;
editorR.create();

/*添加选项事件*/
function addOption(){
	//1 获取到已存在的选项
	var options = $("#option-container").find(".option-item");
	//当前选项索引optionsKey[options.length+1]
	var html = "";
	html += '<div class="form-group option-item">'+
				
				'<label class="col-sm-1 control-label"><span class="option-index">'+optionsKey[options.length+1]+'</span>&nbsp;<input required type="radio" name="answerKey" value="'+optionsKey[options.length+1]+'"></label>'+
				'<div class="col-sm-11">'+
					'<div id="toolbar'+optionsKey[options.length+1]+'" class="toolbar">'+
					'</div>'+
					'<div id="text'+optionsKey[options.length+1]+'" class="text"> <!--可使用 min-height 实现编辑区域自动增加高度-->'+
				    '</div>'+
				 '</div>'+
			'</div>';
	$("#option-container").append(html);
	
	var optionsE = window.wangEditor;
	var editorO = new optionsE('#toolbar'+optionsKey[options.length+1],'#text'+optionsKey[options.length+1]);
	editorO.customConfig.uploadImgMaxSize = 6 * 1024 * 1024;
	editorO.customConfig.uploadImgMaxLength = 5;
	editorO.customConfig.uploadFileName = 'myFileName';
	editorO.customConfig.uploadImgHeaders = {
	       'Accept' : 'multipart/form-data'
	   };
	editorO.customConfig.menus = [//定义菜单
		'head',
		'image',
		'quote'
	];
	editorO.customConfig.uploadImgServer = '/exam/files/upload';
	editorO.customConfig.showLinkImg = false;
	editorO.create();
	
}
/*删除选项*/
function deleteOption(obj){
	if(confirm("确定要删除选项?")){
		var item = $(obj).parents('.option-item');
		var prev = item.prevAll('.option-item');
		var afte = item.nextAll('.option-item');
		for(var i=0;i<afte.length;i++){
			//删除前设置下级兄弟元素的索引
			var option_index = $(afte[i]).find('.option-index');
			$(option_index[0]).text(optionsKey[prev.length+i+1]);
			//设置id
			var toolbar = $(afte[i]).find('.toolbar');
			var text = $(afte[i]).find('.text');
			$(toolbar).prop("id","toolbar"+optionsKey[prev.length+i+1]);
			$(text).prop("id","text"+optionsKey[prev.length+i+1]);
		}
		//删除该选项
		$(item).remove();
	}
}
/* 下拉选择科目加载后台信息 */
$(function() {  
    $(".selectpicker").selectpicker({  
        noneSelectedText : '请选择'  
        });  
  
        $(window).on('load', function() {  
        $('.selectpicker').selectpicker('val', '');  
        $('.selectpicker').selectpicker('refresh');  
        });  
  
        // 下拉数据加载
    $.ajax({
        type : 'get',  
        url :"subject/findAll",  
        dataType : 'json',  
        success : function(datas) {// 返回list数据并循环获取
            var select = $("#slpk");  
            for (var i = 0; i < datas.length; i++) {  
                select.append("<option value='"+datas[i].id+"'>"  
                        + datas[i].name + "</option>");  
            }  
            $('.selectpicker').selectpicker('val', '');  
            $('.selectpicker').selectpicker('refresh'); 
        }  
    });
});
/* 解答题信息提交 */
$("#explanationForm").submit(function(event){
	var id = $('#id').val();
	var num = $('#num').val();
	var subjectId = $('#slpk option:selected').val();
	
	//获取题干
	var title = editor.txt.html();
	if(title==null||title==""||title=="<p><br></p>"){
		alert("题干不能为空！");
		return false;
	}
	
	var referenceStr = editorR.txt.html();
	if(referenceStr==null||referenceStr==""||referenceStr=="<p><br></p>"){
		alert("参考答案不能为空！");
		return false;
	}
	
	$.ajax({
		url:'expl/saveOrUpdate',
		type:'post',
		dataType:'json',
		data:{id:id,num:num,title:title,subjectId:subjectId,referenceStr:referenceStr},
		success:function(data){
			alert(data.msg);
			if(data.success)
				window.location.reload();
		},
		error:function(){
			alert("出错！");
		}
	});
	
	return false;
});
function test(){
	var blank = {};
	var blanks = {};
	
	var id = $('#id').val();
	var num = $('#num').val();
	
	var inputs = $('.the-blank');
	for(var i=0;i<inputs.length;i++){
		$(blanks).attr((i+1),inputs[i].value);
	}
	console.log(JSON.stringify(blanks));
	
	
	
	var content = editor.txt.html();
	console.log(content);
	$("body").append(content);
	alert($('#slpk option:selected').val());
}